package com.rjwh.dingdong.client.activity.more;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.mvp.presenter.impl.MoreChangePasswordPresenter;
import com.rjwh.dingdong.client.mvp.view.MoreChangePasswordView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Kay
 * @ClassName MoreChangePasswordActivity
 * @Description
 * @date 2016-10-19
 * @history 2016-10-19 author: description:
 */
public class MoreChangePasswordActivity extends BaseActivity implements TextWatcher, MoreChangePasswordView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.more_chang_password_old_pwd)
    EditText moreChangPasswordOldPwd;
    @BindView(R.id.more_chang_password_new_pwd)
    EditText moreChangPasswordNewPwd;
    @BindView(R.id.more_chang_password_conform_new_pwd)
    EditText moreChangPasswordConformNewPwd;
    @BindView(R.id.more_chang_password_conform_btn)
    Button moreChangPasswordConformBtn;

    private MoreChangePasswordPresenter presenter;
    private String haspwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_change_password_layout);
        ButterKnife.bind(this);
        initView();
        presenter = new MoreChangePasswordPresenter(this, this);
    }

    private void initView() {
        haspwd = MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.getInstance(), LocalConstant.SP_USER_HASPWD);
        initToolbarHelper();
        if (haspwd.equals("1")) {
            moreChangPasswordOldPwd.setVisibility(View.VISIBLE);
            moreChangPasswordOldPwd.addTextChangedListener(this);
        } else {
            moreChangPasswordOldPwd.setVisibility(View.GONE);
        }
        moreChangPasswordNewPwd.addTextChangedListener(this);
        moreChangPasswordConformNewPwd.addTextChangedListener(this);
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            if (haspwd.equals("1")) {
                getSupportActionBar().setTitle(getResources().getString(R.string.more_account_security_change_password));
            } else {
                getSupportActionBar().setTitle(getResources().getString(R.string.more_account_security_set_password));
            }
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        checkEditText(moreChangPasswordOldPwd.getText().toString(), moreChangPasswordNewPwd.getText().toString(), moreChangPasswordConformNewPwd.getText().toString());
    }

    private void checkEditText(final String oldpwd, final String newpwd, final String confimpwd) {
        if (haspwd.equals("1")) {
            if (AsStrUtils.isEmpty(oldpwd) || AsStrUtils.isEmpty(newpwd) || AsStrUtils.isEmpty(confimpwd)) {
                moreChangPasswordConformBtn.setBackgroundResource(R.drawable.long_btn_unable);
                moreChangPasswordConformBtn.setClickable(false);
            } else {
                moreChangPasswordConformBtn.setBackgroundResource(R.drawable.long_btn);
                moreChangPasswordConformBtn.setClickable(true);
                moreChangPasswordConformBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (newpwd.equals(confimpwd)) {
                            //修改密码
                            presenter.doUpdatePwd(oldpwd, newpwd);
                        } else {
                            showToast("两次密码不一致！");
                        }
                    }
                });
            }
        } else {
            if (AsStrUtils.isEmpty(newpwd) || AsStrUtils.isEmpty(confimpwd)) {
                moreChangPasswordConformBtn.setBackgroundResource(R.drawable.long_btn_unable);
                moreChangPasswordConformBtn.setClickable(false);
            } else {
                moreChangPasswordConformBtn.setBackgroundResource(R.drawable.long_btn);
                moreChangPasswordConformBtn.setClickable(true);
                moreChangPasswordConformBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (newpwd.equals(confimpwd)) {
                            //设置密码
                            presenter.doUpdatePwd("", newpwd);
                        } else {
                            showToast("两次密码不一致！");
                        }
                    }
                });
            }
        }
    }


    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadDialog();
    }

    @Override
    public void LoadData() {
        MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_USER_HASPWD, "1");
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
